package com.project.controller;

import com.project.dao.IMeituanDao;
import com.project.dto.CinemaDto;
import com.project.dto.OrderDto;
import com.project.service.CinemaRemoteService;
import com.project.util.RedisCacheUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @Author(王思翰)
 * @Description(类的描述)
 * @Date(创建时间) 2021/9/7 16:57
 */
@RestController
@RequestMapping("meituan")
public class MeituanController {
    @Autowired
    CinemaRemoteService cinemaRemoteService;
//    @Autowired
//    RabbitTemplate rabbitTemplate;
    @Autowired
    IMeituanDao meituanDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedisCacheUtil redisCacheUtil;

    /**
     * 回调方法
     */
    RabbitTemplate.ConfirmCallback callback = (CorrelationData correlationData, boolean b, String s) -> {
        //投递成功
        if (b) {
            //获得关联数据,movieId
            String orderIdStr = correlationData.getId();
            int orderId = Integer.parseInt(orderIdStr);
            //把订单状态改为1
//            OrderEntity orderEntity = orderDao.findById(orderId).get();
//            orderEntity.setTag(1);
//            orderDao.save(orderEntity);

            System.out.println("发送信息确认"+meituanDao.findById(orderId));

        }
    };

    @GetMapping("findByAll")
    public List<CinemaDto> findByAll() {
        List<CinemaDto> list = cinemaRemoteService.findByAll();
        for (CinemaDto cinema : list) {
            CinemaDto cinemaDto = meituanDao.findByNane(cinema.getName());
            if (cinemaDto == null) {
                meituanDao.addCinema(cinema);
            }
        }
        return list;
    }

    @GetMapping("addOrder/{id}/{name}/{money}")
    public String addOrder(@PathVariable("id") int id
            , @PathVariable("name") String name
            , @PathVariable("money") int money) {
        OrderDto orderDto = new OrderDto();
        orderDto.setCid(id);
        orderDto.setCname(name);
        orderDto.setCmoney(money);
        meituanDao.addOrder(orderDto);
        cinemaRemoteService.addOrder(id, name, money);
        redisTemplate.opsForSet().add("order",orderDto);
        redisCacheUtil.expire("order",30);
        return "添加订单成功";
    }


    @GetMapping("findByOrderAll")
    public List<OrderDto> findByOrderAll() {
        System.out.println(redisCacheUtil.getExpire("order"));
        return meituanDao.findByAll();
    }


    @GetMapping("findByRadisOrderAll")
    public Set<OrderDto> findByRadisOrderAll() {
        return redisTemplate.opsForSet().members("order");
    }

    @GetMapping("chupiao/{id}")
    public String chupiao(@PathVariable("id") int id) throws InterruptedException {

//        //开启手动确认
//        rabbitTemplate.setMandatory(true);//开启消息确认
//        //绑定回调函数
//        rabbitTemplate.setConfirmCallback(callback);

        OrderDto orderDto = meituanDao.findById(id);
        System.out.println("出票："+orderDto);
        if (orderDto.getStatus().equals("未出票")) {
            meituanDao.update(id);
            meituanDao.updateDianyingyuan(id);
//            redisTemplate.opsForValue().set("orderDto",orderDto);
            redisTemplate.opsForSet().add("orderDto",orderDto);
            if (redisCacheUtil.expire("orderDto",60)){
                return "修改成功";
            }
//            //组装关联数据
//            CorrelationData correlationData = new CorrelationData(String.valueOf(orderDto.getId()));
//            System.out.println("在出票中开启手动确认和绑定回调");
//            rabbitTemplate.convertAndSend("meituanDirect", "meituan.Direct", orderDto, correlationData);
//            Thread.sleep(500);
        }
        return "该订单以出票";
    }

//    @RabbitListener(queues = "meituanQueue")
    public void updateNumber() {
//        System.out.println("手动确认中打印"+orderDto);


//        int i=1/0;//模拟在处理业务是出现异常

        //确认收到，如果没有此行代码，那么就算收到了信息，也会标记为等待验证状态
//        channel.basicAck(tag,false);
    }
}
